package com.spontaneappnew;

import java.util.ArrayList;
import java.util.List;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class Categories {
	
	 public static final int TYPE_NORMAL = 0;
	 public static final int TYPE_SELECT = 1;
	/*
	 * Categories Singleton Implementation
	 * Each Category has an ID(String),name(String),icon(Image) and selected(boolean) fields.
	 */
	public int numOfSelected = 0;
	private static Categories instance = null;
	private ArrayList<Category> categories;
	
	private Categories(){
		this.categories = new ArrayList<Category>();
		this.categories.add(new Category("All Categories"));
		ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Categories").orderByAscending("name");
		List<ParseObject> Categories = null;
		try {
			Categories = query.find();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		for (ParseObject category : Categories) {
			byte[] icon=null;
			byte[] marker = null;
			try {
				icon = category.getParseFile("icon").getData();
				marker = category.getParseFile("marker").getData();
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			categories.add(new Category(category.getObjectId(), category.getString("name"),icon,marker));
	   }
	}
	
	public static Categories getInstance(){
		if (instance==null)
			instance = new Categories();
		return instance;
	}
	
	public ArrayList<Category> getCategories(){
		return categories;
	}
	
	public void setCategories(ArrayList<Category> categories){
		this.categories = categories;
	}
	
	public boolean removeFromSelected(String id){
		for (Category category : categories) {
			if (category.getType()==TYPE_SELECT && category.getID()==id){
				categories.remove(category);
				return true;
			} else {
				if (category.getType()==TYPE_NORMAL)
					return false;
			}
		}
		return false;
	}
	
	public int getOffset(){
		return numOfSelected;
	}
	
	public void incOffset(){
		numOfSelected++;
	}
	
	public void decOffset(){
		numOfSelected--;
	}
	
	public void setOffset(int newOffset){
		numOfSelected = newOffset;
	}
	
	public List<Category> getAllCategories(){
		List<Category> result;
		if(numOfSelected == 0)
			result = categories.subList(1, categories.size());
		else
			result = categories.subList(numOfSelected+2, categories.size());
		return result;
	}
	
	public boolean isAnySelected(){
		return !(numOfSelected==0);
	}
	
	public List<Category> getSelectedCategories(){
		List<Category> result;
		if (numOfSelected==0)
			return null;
		else {
			result = this.categories.subList(1, numOfSelected+1);
			return result;
		}
	}
	
	/*
	 * Gets a category id and returns the category with that id
	 */
	public Category getCategoryById(String id){
		Category res = null;
		int start;
		if(numOfSelected == 0)
			start = 1;
		else
			start = numOfSelected+2;
		for (int i = start; i < categories.size(); i++) {
			if(categories.get(i).getID().compareTo(id) == 0)
				return res = categories.get(i);
		}
		return res;
	}
}